package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.format.annotation.DateTimeFormat;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;


@RestController
@RequestMapping("/admin/report")
@Tag(name = "数据统计相关接口")
@Slf4j
public class ReportController {

    @Autowired
    private ReportService reportService;

    /**
     * 营业额统计
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/turnoverStatistics")
    @Operation(summary = "营业额统计")
    public Result<TurnoverReportVO> turnoverStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") @NotNull(message = "开始时间不能为空") LocalDate begin,
                                                       @DateTimeFormat(pattern = "yyyy-MM-dd") @NotNull(message = "结束时间不能为空") LocalDate end) {
        log.info("营业额统计，参数：{}，{}", begin, end);

        TurnoverReportVO turnoverReportVO = reportService.getTurnoverStatistics(begin, end);

        return Result.success(turnoverReportVO, MessageConstant.GET_TURNOVER_REPORT_SUCCESS);
    }

    /**
     * 销量排名top10
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/top10")
    @Operation(summary = "查询销量排名top10")
    public Result<SalesTop10ReportVO> getSalesTop10(@DateTimeFormat(pattern = "yyyy-MM-dd") @NotNull(message = "开始时间不能为空") LocalDate begin,
                                                    @DateTimeFormat(pattern = "yyyy-MM-dd") @NotNull(message = "结束时间不能为空") LocalDate end) {
        log.info("查询销量排名top10，参数：{}，{}", begin, end);
        SalesTop10ReportVO salesTop10ReportVO = reportService.getSalesTop10(begin, end);
        if (StringUtils.isBlank(salesTop10ReportVO.getNameList())){
            return Result.success(MessageConstant.NO_SALES_DATA_FOUND);
        }
        return Result.success(salesTop10ReportVO, MessageConstant.GET_SALES_TOP10_SUCCESS);
    }

    /**
     * 订单量统计
     * @return
     */
    @GetMapping("/ordersStatistics")
    @Operation(summary = "订单量统计")
    public Result<OrderReportVO> ordersStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") @NotNull(message = "开始时间不能为空") LocalDate begin,
                                                  @DateTimeFormat(pattern = "yyyy-MM-dd") @NotNull(message = "结束时间不能为空") LocalDate end) {
        OrderReportVO orderReportVO = reportService.ordersStatistics(begin,end);
        return Result.success(orderReportVO, MessageConstant.GET_ORDERS_STATISTICS_SUCCESS);
    }

    /**
     * 用户统计
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/userStatistics")
    @Operation(summary = "用户统计")
    public Result<UserReportVO> getUserStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") @NotNull(message = "开始时间不能为空") LocalDate begin,
                                                 @DateTimeFormat(pattern = "yyyy-MM-dd") @NotNull(message = "结束时间不能为空") LocalDate end) {
        UserReportVO userReportVO = reportService.getUserStatistics(begin,end);
        return Result.success(userReportVO, MessageConstant.GET_USER_STATISTICS_SUCCESS);
    }

    /**
     * 导出Excel报表
     * @return
     */
    @GetMapping("/export")
    @Operation(summary = "导出Excel报表")
    public void export(HttpServletResponse response){
        reportService.exportBusinessData(response);
    }
}
